import React, { useEffect } from "react";
import { BackHandler, View, SafeAreaView } from "react-native";
import LoginComponent from "@/components/Login/index";
import TokenManager from "@/utils/token-utils";
import { router } from "expo-router";
import { useTheme } from "@/contexts/ThemeContext";

export default function Login() {
  const { colors } = useTheme();

  // 监听硬件返回键
  useEffect(() => {
    const onBackPress = () => {
      if (!router.canGoBack()) {
        BackHandler.exitApp();
        return true;
      }
      return false;
    };

    const backHandler = BackHandler.addEventListener(
      "hardwareBackPress",
      onBackPress
    );

    return () => backHandler.remove();
  }, [router]);

  // 自动跳转逻辑
  useEffect(() => {
    if (TokenManager.getToken()) {
      router.replace("/");
    }
  }, []);

  return (
    <SafeAreaView
      style={{
        flex: 1,
        backgroundColor: colors.background,
      }}
    >
      <LoginComponent />
    </SafeAreaView>
  );
}
